package com.foo.servicesCall;


import org.apache.axis2.AxisFault;

import com.foo.servicesCall.EventsStub.Event;
import com.foo.servicesCall.EventsStub.EventsArray;
import com.foo.servicesCall.EventsStub.GetEventsByDate;
import com.foo.servicesCall.EventsStub.GetEventsByDateResponse;
import com.foo.servicesCall.EventsStub.GetEventsByKeyword;
import com.foo.servicesCall.EventsStub.GetEventsByKeywordResponse;

import com.foo.servicesCall.EventsStub.GetAllEvents;
import com.foo.servicesCall.EventsStub.GetAllEventsResponse;

import java.rmi.RemoteException;

public class EventWSClient 
{
		
		private static final String SERVICE_END_POINT = "http://localhost:8080/axis2/services/Events";
		private EventsStub stub;

		public EventWSClient()
		{
			try
			{
				stub = new EventsStub(SERVICE_END_POINT);
			} catch (AxisFault e) {
				System.out.println("Error while initializing stub!");
				e.printStackTrace();
			}
		}
		
		public EventsArray getAllEvents()
		{
			try
			{
				stub = new EventsStub(SERVICE_END_POINT);
			GetAllEventsResponse response = new GetAllEventsResponse();
			GetAllEvents data = new GetAllEvents();
			
			data.setIn("");
			
			try
			{
				response = stub.getAllEvents(data);
			}
			catch(RemoteException e)
			{
				System.out.println(e.getMessage());
			}
			catch(GetAllEventsFaultException ex)
			{
				System.out.println(ex.getMessage());
			}
			return response.getOut();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			return null;
		}
		
		
		public EventsArray getEventsByKeyword(String keyword) 
		{
			try
			{
			GetEventsByKeywordResponse response = new GetEventsByKeywordResponse();
			GetEventsByKeyword data = new GetEventsByKeyword();
			
			data.setKeyword(keyword);
			
			try
			{
				response = stub.getEventsByKeyword(data);
			}
			catch(RemoteException e)
			{
				System.out.println(e.getMessage());
			}
			catch(GetEventsByKeywordFaultException ex)
			{
				System.out.println(ex.getMessage());
			}
			return response.getOut();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			return null;
		}
		
		public EventsArray getEventsByDate(String start, String end) 
		{
			try
			{
			GetEventsByDateResponse response = new GetEventsByDateResponse();
			GetEventsByDate data = new GetEventsByDate();
			
			data.setStart(start);
			data.setEnd(end);
			
			try
			{
				response = stub.getEventsByDate(data);
			}
			catch(RemoteException e)
			{
				System.out.println(e.getMessage());
			}
			catch(GetEventsByDateFaultException ex)
			{
				System.out.println(ex.getMessage());
			}
			return response.getOut();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			return null;
		}
}
